<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript" src="../js/global.js"></script>
	</head>
	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
			<legend>系統功能模块维护</legend>
		</fieldset>
		<div style="display: none;">
			<input type="hidden" id="stestPaperName" />
			<input type="hidden" id="sstudentName" />
			<input type="hidden" id="sstudentId" />
			<input type="hidden" id="stestPaperId" />
			<input type="hidden" id="sscoreId" />
		</div>
		<!-- 搜索区 -->
		<div id="searchePage" class="layui-form-item" style="margin-top: 10px;margin-left: 10px;">
			<form class="layui-form" method="post" onsubmit="return false;">
				<label class="layui-form-mid">考场号:</label>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" id="examRoomNo" name="examRoomNo" autocomplete="off" class="layui-input" />
				</div>
				<label class="layui-form-mid">地址:</label>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" id="address" name="address" autocomplete="off" class="layui-input" />
				</div>
				<label class="layui-form-mid">考场状态:</label>
				<div class="layui-input-inline" style="width: 100px;">
					<select id="status" name="status">
						<option></option>
						<option value="0">空闲</option>
						<option value="1">占用</option>
					</select>
				</div>
				<div class="layui-input-inline" style="width: 70px;">
					<button class="layui-btn" id="search" data-type="reload">搜索</button>
				</div>
				<div class="layui-input-inline">
					<button class="layui-btn" lay-event="createClass" id="addPage">添加</button>
				</div>
			</form>
		</div>
		<!--
			编辑弹层
		-->
		<div id="updatePage" style="display: none;">
			<form class="layui-form" action="" onsubmit="return false" lay-filter="updateForm">
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-label"><span style="color: red;">*</span>考场号:</label>
					<div class="layui-input-inline" style="width: 350px;">
						<input type="hidden" name="examRoomId" />
						<input type="text" name="examRoomNo" class="layui-input" />
					</div>
				</div>
				<div class="layui-form-item" style="margin-top: 30px;">
					<label class="layui-form-label"><span style="color: red;">*</span>地址:</label>
					<div class="layui-input-inline" style="width: 350px;">
						<input type="text" name="address" class="layui-input" />
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label"><span style="color: red;">*</span>考场状态:</label>
					<div class="layui-input-inline" style="width: 350px;">
						<select class="sc" name="status">
							<option></option>
							<option value="0">空闲</option>
							<option value="1">占用</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label"><span style="color: red;">*</span>所属学校:</label>
					<div class="layui-input-inline" style="width: 350px;">
						<select class="sc" name="schoolId">
							<option></option>
						</select>
					</div>
				</div>
				<div class="layui-btn-container" style="text-align: center;">
					<button class="layui-btn" id="transmit" lay-submit="" lay-filter="updateClass">保存</button>
				</div>
			</form>
		</div>
		<!--
        	数据表格
        -->

		<table class="layui-hide" id="class_table" lay-filter="test">

		</table>
		<!--行内工具栏-->
		<script type="text/html" id="barDemo">


	{{#  if(d.studentExam.status==0){ }}
	  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">批阅</a>
	 {{# } else { }}
	  <a class="layui-btn  layui-btn-xs" lay-event="detail">查看</a>
	{{# } }}
   	
   </script>
		<script type="text/html" id="statue">
	 {{# if(d.studentExam.status==0){ }}
	   <span style="color: red;font-weight: bolder;">未批改</span>	 
	  {{# } else { }}
	     <span style="color: green;font-weight: bolder;">已批改</span>	 
	  {{# } }}
	  
	  </script>
		<script type="text/html" id="scoreStatus">
	  {{# if(d.scoreStatus==0){ }}
	   <span style="color: green;font-weight: bolder;">正常</span>	
	  {{# } else if(d.scoreStatus==1) { }},
	      <span style="color: darkcyan;font-weight: bolder;">缺考</span>	
	   {{# } else { }}
	      <span style="color: red;font-weight: bolder;">作弊</span>	
	    {{# } }}
	    
	    </script>


	</body>
	<script>
		layui.use(['table', 'form', 'layer', 'laydate', 'upload', 'element'], function() {
			var layer = layui.layer;
			var form = layui.form;
			var table = layui.table;
			var laydate = layui.laydate;
			var upload = layui.upload;
			var element = layui.element;
			var $ = layui.$;
			//渲染用户表
			$.get(globalData.examinationServer + '/score/listScores', function(res) {
				var html = "";
				for (var i = 0; i < res.data.length; i++) {
					html += "<option value=" + res.data[i].schoolId + ">" + res.data[i].schoolName + "</option>"

				}
				$(".sc").append(html);

				form.render('select')
			}, "json")

			//渲染数据表格
			table.render({
				elem: '#class_table',
				height: 400,
				url: globalData.examinationServer + '/score/listScores',
				method: "get",
				//headers: {token: globalData.getCurToken()},//设置请求头
				title: "用户表",
				page: true,
				cols: [
					[{
							field: 'testPaperId',
							title: '试卷',
							templet: '<div>{{testPaper(d.testPaperId)}}</div>'
						},
						{
							field: 'studentId',
							title: '考生',
							templet: '<div>{{student(d.studentId)}}</div>'
						}, {
							field: 'scoreStatus',
							title: '考试状态',
							templet: $("#scoreStatus")
						}, {
							field: 'score',
							title: '最终成绩',
							sort: true,
							align: 'center',
						}, {
							field: 'checkUserId',
							title: '改卷老师',
							templet: '<div>{{checkUser(d.checkUserId)}}</div>'
						}, {
							field: 'statue',
							title: '批改状态',
							templet: $("#statue")
						}, {
							title: '操作',
							align: "center",
							toolbar: '#barDemo'
						}
					]
				],
				id: "testReload",
				done: function(res, curr, count) {

				}
			});

			//添加班级弹出层
			$("#addPage").on('click', function() {
				layer.open({
					type: 2,
					title: '添加班级',
					area: ['500px', '400px'],
					content: 'examRoom_add.html',
					end: function() {
						//alert(1)
					}
				});
			});


			active = {
				reload: function() {
					var examRoomNo = $("#examRoomNo");
					var address = $("#address");
					var status = $("#status");
					table.reload('testReload', {
						where: {
							'examRoomNo': examRoomNo.val(),
							'address': address.val(),
							'status': status.val()
						},
						curr: {
							page: 1
						}
					});
				}
			};

			//查询操作
			$("#search").on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			})


			table.on('row(test)', function(obj) {
				$("#stestPaperName").val(obj.tr[0].cells[0].innerText);
				$("#sstudentName").val(obj.tr[0].cells[1].innerText);


				console.log(obj.data);
			})
			//			
			//行内工具栏监听
			table.on('tool(test)', function(obj) {
				var data = obj.data;
				switch (obj.event) {
					case 'detail':
						//将学号赋值给输入框，以便将值传入到子页面sscoreId
						$("#sstudentId").val(data.studentId);
						$("#stestPaperId").val(data.testPaperId);
						$("#sscoreId").val(data.scoreId);

						form.val('updateForm', data);
						var update_index = layer.open({
							type: 2,
							title: '您正在修改该的信息',
							area: ['1400px', '700px'],
							content: 'testPaperDetail/vip_ming_do _detail.html',
							success: function(layero, index) {
								//修改提交
								form.on('submit(updateClass)', function(obj) {
									//console.log(obj.field);
									$.ajax({
										type: "put",
										url: globalData.examinationServer + '/examRoom/updateExamRoom',
										data: obj.field, //需要传递到服务端的数据
										dataType: 'json', //服务端响应数据类型
										xhrFields: {
											withCredentials: true
										},
										// beforeSend: function(request) {
										// 	request.setRequestHeader("token", globalData.getCurToken());
										// },
										success: function(res) {
											if (res.code == 0) {
												layer.msg(res.msg, {
													icon: 1,
													time: 2000
												}, function(index) {
													layer.close(update_index);
													table.reload('testReload');
												});

											} else {
												layer.msg(res.msg, {
													icon: 2,
													time: 2000
												});
											}
										}
									});
								});
							}
						});

						break;
					case 'update':
						//将学号赋值给输入框，以便将值传入到子页面sscoreId
						$("#sstudentId").val(data.studentId);
						$("#stestPaperId").val(data.testPaperId);
						$("#sscoreId").val(data.scoreId);

						form.val('updateForm', data);
						var update_index = layer.open({
							type: 2,
							title: '您正在修改该的信息',
							area: ['1400px', '700px'],
							content: 'testPaperDetail/vip_ming_do.html',
							success: function(layero, index) {
								//修改提交
								form.on('submit(updateClass)', function(obj) {
									//console.log(obj.field);
									$.ajax({
										type: "put",
										url: globalData.examinationServer + '/examRoom/updateExamRoom',
										data: obj.field, //需要传递到服务端的数据
										dataType: 'json', //服务端响应数据类型
										xhrFields: {
											withCredentials: true
										},
										// beforeSend: function(request) {
										// 	request.setRequestHeader("token", globalData.getCurToken());
										// },
										success: function(res) {
											if (res.code == 0) {
												layer.msg(res.msg, {
													icon: 1,
													time: 2000
												}, function(index) {
													layer.close(update_index);
													table.reload('testReload');
												});

											} else {
												layer.msg(res.msg, {
													icon: 2,
													time: 2000
												});
											}
										}
									});
								});
							}
						});

						break;
				}
			});


		});

		//应用template模板，渲染考生
		function student(studentId) {

			var sId = studentId;
			var title;
			$.ajax({
				url: globalData.permissionServer + '/user/listUsers', //你的后台请求路径
				dataType: 'json',
				async: false, //同步
				success: function(data) {
					$.each(data.data, function(index, obj) {

						if (obj.userId == sId) {
							title = obj.userName;

						}
					});
				}
			});
			return title;
		};
		//应用template模板，渲染试卷
		function testPaper(testPaperId) {

			var tId = testPaperId;
			var title;
			$.ajax({
				url: globalData.examinationServer + '/testPaper/listTestPapers', //你的后台请求路径
				dataType: 'json',
				async: false, //同步
				success: function(data) {
					$.each(data.data, function(index, obj) {
						//
						if (obj.testPaperId == tId) {


							title = obj.testPaperName;

						}
					});
				}
			});
			return title;
		};
		//应用template模板，渲染改卷老师
		function checkUser(checkUserId) {
			// alert(123);
			var cId = checkUserId;
			var title;
			$.ajax({
				url: globalData.permissionServer + '/user/listUsers', //你的后台请求路径
				data: {
					page: 3
				},
				dataType: 'json',
				async: false, //同步
				success: function(data) {
					console.log(data.data.length);
					$.each(data.data, function(index, obj) {
						console.log(obj.userId);
						if (obj.userId == cId) {
							title = obj.userName;

						} else {
							title = "";
						}
					});
				}
			});
			return title;
		};
	</script>
</html>
